package com.parkcar;

import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;

import java.util.List;

import adapter.PoiListAdapter;
import util.BaseActivity;
import util.BaseTools;
import util.Constrants;

/**
 * Created by Administrator on 2016/3/9.
 */
public class SearchActivity extends BaseActivity implements PoiSearch.OnPoiSearchListener, TextWatcher {
    private ListView mPoiSearchList;
    private EditText search_address;
    private PoiSearch.Query query;// Poi查询条件类
    private PoiSearch poiSearch;// POI搜索
    private PoiListAdapter mpoiadapter;
    @Override
    protected void setUpui(Bundle arg0) {
        setContentView(R.layout.activity_search);
        setTitleGone(View.GONE);
        search_address = (EditText) findViewById(R.id.search_address);
        mPoiSearchList = (ListView) findViewById(R.id.locations);
        search_address.addTextChangedListener(this);
        mPoiSearchList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                if(mpoiadapter==null||mpoiadapter.getCount()<=i){return;}
                PoiItem poiItem = (PoiItem) mpoiadapter.getItem(i);
                Intent intent = new Intent();
                intent.putExtra("lat",poiItem.getLatLonPoint().getLatitude());
                intent.putExtra("lng",poiItem.getLatLonPoint().getLongitude());
                setResult(Constrants.SEARCH,intent);
                finish();
            }
        });
    }

    @Override
    protected void updateUI(boolean isLogin) {

    }

    /**
     * 开始进行poi搜索
     */
    protected void doSearchQuery(String keyWord) {
        query = new PoiSearch.Query(keyWord, "", "杭州");// 第一个参数表示搜索字符串，第二个参数表示poi搜索类型，第三个参数表示poi搜索区域（空字符串代表全国）
        query.setPageSize(10);// 设置每页最多返回多少条poiitem
        query.setPageNum(0);// 设置查第一页
        poiSearch = new PoiSearch(this, query);
        poiSearch.setOnPoiSearchListener(this);
        poiSearch.searchPOIAsyn();
    }

    @Override
    public void onPoiItemSearched(PoiItem item, int rCode) {
       /* if (rCode == 0) {
            List<PoiItem> poiItems = new ArrayList<PoiItem>();
            poiItems.add(item);
            mpoiadapter=new PoiListAdapter(this, poiItems);
            mPoiSearchList.setAdapter(mpoiadapter);
        }*/
    }

    @Override
    public void onPoiSearched(PoiResult result, int rcode) {
        if (rcode == 0) {
            if (result != null ) {
                List<PoiItem> poiItems = result.getPois();
                mpoiadapter=new PoiListAdapter(this, poiItems);
                mPoiSearchList.setAdapter(mpoiadapter);
            }
        }
    }

    @Override
    public void afterTextChanged(Editable s) {

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
                                  int after) {

    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        String newText = s.toString().trim();
        if(!BaseTools.isValueEmpty(newText)){
        doSearchQuery(newText);}

    }
    public void to_cancel(View viwe){
        finish();
    }
}
